<?php

namespace app\admin\controller\roomcardcode;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Clubdiamond extends Backend
{   
    
    /**
     * VAnnalRoom模型对象
     */
    protected $model = null;

    public function _initialize()
    { 
        parent::_initialize();
        $this->model = model('CClubDiamondLog');
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        //进行获取信息
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {   
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            /*
             * 统计数据
             */
            $statistics = [];
            $sum_list = Db::table('fa_c_club_diamond_log')->field('type,sum(diamond) as diamond')->where($where)->group('type')->select();//房卡补充总量
            $statistics['type_one'] = $statistics['type_two'] = $statistics['type_three'] = 0;
            foreach ($sum_list as $key=>$val){
                if($val['type'] == 1){
                    $statistics['type_one'] =$val['diamond'];
                }
                if($val['type'] == 2){
                    $statistics['type_two'] =$val['diamond'];
                }
                if($val['type'] == 3){
                    $statistics['type_three'] =$val['diamond'];
                }
            }

            //总数
            $total = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->count();
           
            $list = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();  
             
            //处理数据
            $clubid_arr = $uid_arr = $user_arr = $club_arr = $diamond_arr = array();
            foreach ($list as $val){
                $clubid_arr[] = $val['clubid'];//俱乐部ID号
            }
            $club_list = Db::table('fa_c_club_examine')->where(['clubid'=>array('in',$clubid_arr)])->select();//俱乐部列表
            foreach ($club_list as $val){
                $uid_arr[] = $val['owner'];//管理员ID号
                $club_arr[$val['clubid']] = $val;
            }

            $user_list = Db::connect("GameDB")->table('user')->field('uid,nickname')->where(['uid'=>array('in',$uid_arr)])->select();//管理员列表
            foreach ($user_list as $val){
                $user_arr[$val['uid']] = $val['nickname'];
            }
            
            $diamond_list = Db::connect("GameDB")->table('club')->field('id,diamond')->where(['id'=>array('in',$clubid_arr)])->select();//俱乐部列表
            foreach ($diamond_list as $val){
                $diamond_arr[$val['id']] = $val['diamond'];
            }

            foreach($list as $key=>$val){
                $list[$key]['name'] = isset($club_arr[$val['clubid']])?$club_arr[$val['clubid']]['name']:'-';//俱乐部名称
                $list[$key]['owner'] = isset($club_arr[$val['clubid']])?$club_arr[$val['clubid']]['owner']:'-';//管理员ID
                $list[$key]['ownername'] = isset($user_arr[$list[$key]['owner']])?$user_arr[$list[$key]['owner']]:'-';//管理员昵称
                $list[$key]['newdiamond'] = isset($diamond_arr[$val['clubid']])?$diamond_arr[$val['clubid']]:'0';//剩余房卡
            }

            //关联查询数据       
            $result = array("total" => $total, "rows" => $list,"statistics"=>$statistics);

            return json($result);
        }

        return $this->view->fetch();
    }    
    
}
